package com.ltsznh.android.familybooks.category;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ltsznh.android.familybooks.R;
import com.ltsznh.android.familybooks.db.dbo;

public class Category2Activity extends Activity {

    private ArrayAdapter<String> category2Adapter;
    private ListView category2ListView;
    private String category;
    private String invType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category2);

        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        category2Adapter = new ArrayAdapter<String>(this, R.layout.category);
        category2ListView = (ListView)findViewById(R.id.listView_category2);
        category2ListView.setAdapter(category2Adapter);

        category = getIntent().getStringExtra("category");
        invType = getIntent().getStringExtra("invType");

        ((TextView)findViewById(R.id.textView_category2_category)).setText(category);
        refreshData();

        initCategory2Edit();
    }

    public void refreshData(){
        dbo.getReadableDatabase(this);
        String sqlString = "SELECT distinct invCategory2 FROM inv_category where invCategory='" + category + "' order by displayOrder";
//        Log.v("TTT",sqlString);
        Cursor category2Cursor = dbo.fbSQLite.rawQuery(sqlString, null);
        category2Adapter.clear();
        if(category2Cursor.getCount()>0){
            category2Cursor.moveToFirst();
            do{
                category2Adapter.add(category2Cursor.getString(0));
            }while (category2Cursor.moveToNext());
        }
        category2Cursor.close();
        dbo.closeDatabase();
    }

    protected void initCategory2Edit(){
        category2ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                String category2 = category2Adapter.getItem(i).toString();
                Category2EditDialogFragment categoryEditDialog = Category2EditDialogFragment.newInstance(category2);
                categoryEditDialog.setArgs(Category2Activity.this,"inv_category","invCategory2","invCategory2 = '" + category2 + "'");
                categoryEditDialog.show(getFragmentManager(), null);
                return false;
            }
        });
    }

    protected void addCategory2(){
        Category2AddDialogFragment categoryAddDialog = Category2AddDialogFragment.newInstance(category);
        categoryAddDialog.setArgs(Category2Activity.this,"inv_category",invType,"invType","invCategory","invCategory2");
        categoryAddDialog.show(getFragmentManager(), null);
    }

    protected void goBack(){
        Intent intent = new Intent(this, CategoryActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                goBack();
                return true;
            case R.id.action_add:
                addCategory2();
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.category2, menu);
        return true;
    }
    
}
